using System;
using System.Collections.Generic;
using CMS_Admin.Backend.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using CMS_Admin.Backend.Api.ParamModel;
using Newtonsoft.Json;
using CMS_Admin.Backend.Api.Repository;
using CMS_Admin.Backend.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;

namespace CMS_Admin.Backend.Controllers
{

    [ApiController]
    [Route("homeController")]
    public class HomeController : ControllerBase
    {

        private IRepository<ArticleClass> _articleClass;
        private IRepository<Articles> _articles;
        private readonly IConfiguration _configuration;


        public HomeController(IConfiguration configuration, IRepository<ArticleClass> articleClass, IRepository<Articles> articles)
        {

            _articleClass = articleClass;
            _configuration = configuration;
            _articles = articles;


        }

        /// <summary>
        /// 查找分组后的id和栏目名称
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic Get()
        {

            // var articleClass = _articleClass.Table.OrderByDescending(art => art.Id);

            var article = from articles in _articles.Table
                          join ac in _articleClass.Table on articles.ArticleClassId equals ac.Id
                          group articles by new { articles.ArticleClassId, ac.Id, ac.ArticleClassName }
                          into a
                          select new { ArticleClassId = a.Key.Id, Count = a.Count(), ArticleClassName = a.Key.ArticleClassName };


            var res = new
            {
                Code = 200,
                Data = article,

                Msg = "获取栏目列表成功^_^"
            };
            return res;
        }

    }
}